package com.ygqh.baby.controller.admin;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import sun.misc.BASE64Decoder;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.foxinmy.weixin4j.type.TradeType;
import com.foxinmy.weixin4j.util.StringUtil;
import com.ygqh.baby.ao.CodeType;
import com.ygqh.baby.ao.FileType;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.Message.Type;
import com.ygqh.baby.ao.OrderDetailStatus;
import com.ygqh.baby.ao.OrderOperation;
import com.ygqh.baby.ao.OrderStatus;
import com.ygqh.baby.ao.OrderType;
import com.ygqh.baby.ao.PayType;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.ao.WdtTradeStatus;
import com.ygqh.baby.controller.utils.DocumentHandler;
import com.ygqh.baby.model.YgOrder4ImportModel;
import com.ygqh.baby.model.YgPtSkuInfo;
import com.ygqh.baby.model.logistics;
import com.ygqh.baby.model.wdt.WdtOrderResData;
import com.ygqh.baby.po.AdminUser;
import com.ygqh.baby.po.WdtQybStockOut;
import com.ygqh.baby.po.YgLogistics;
import com.ygqh.baby.po.YgOrder;
import com.ygqh.baby.po.YgOrderDetail;
import com.ygqh.baby.po.YgOrderOperation;
import com.ygqh.baby.po.YgOrderTrack;
import com.ygqh.baby.service.FileService;
import com.ygqh.baby.service.ScheduleJobService;
import com.ygqh.baby.service.WdtQybStockOutService;
import com.ygqh.baby.service.YgFrameCodeService;
import com.ygqh.baby.service.YgLogisticsService;
import com.ygqh.baby.service.YgNgmmService;
import com.ygqh.baby.service.YgOrderDetailService;
import com.ygqh.baby.service.YgOrderLogService;
import com.ygqh.baby.service.YgOrderOperationService;
import com.ygqh.baby.service.YgOrderPaymentService;
import com.ygqh.baby.service.YgOrderService;
import com.ygqh.baby.service.YgOrderTrackService;
import com.ygqh.baby.service.YgSkuService;
import com.ygqh.baby.service.ngmm.util.NgmmConfigure;
import com.ygqh.baby.service.task.WdtService;
import com.ygqh.baby.service.task.YgAsynClient;
import com.ygqh.baby.service.wechat.YgWeChatService;
import com.ygqh.baby.service.wechat.protocol.UnifiedOrderReqData;
import com.ygqh.baby.shiro.utils.SessionUtil;
import com.ygqh.baby.utils.DateConvertUtils;
import com.ygqh.baby.utils.QRCodeUtil;
import com.ygqh.baby.utils.WebUtils;

@Controller
@RequestMapping("/admin/trackorder")
public class TrackOrderController extends BaseController<Object, java.lang.Long> {
	@Autowired
	private YgOrderTrackService ygOrderTrackService;
	@Autowired
	private WdtQybStockOutService wdtQybStockOutService;
	@Autowired
	private FileService fileService;
	
	
	@RequestMapping("searchOrderTrack")
	@ResponseBody
	public Map<String, Object> searchOrderTrack(String q, QueryInfo queryInfo, Date startDate,Date endDate,Boolean isFinished,String srcId,String workType ) {
		
		ResultSet<YgOrderTrack> resultSet =ygOrderTrackService.search(queryInfo, q,startDate,endDate,isFinished,srcId,workType);
		return resultSet.toDatatables(null);
		
	}
	@RequestMapping("getStockOut")
	@ResponseBody
	public WdtQybStockOut getStockOut(Long logisticsNo){
		
		return wdtQybStockOutService.findStockOutByLogisticsNo(logisticsNo);
	}
	@RequestMapping("saveTrackOrder")
	@ResponseBody
	public Message saveTrackOrder(String trackOrder,String trackOrderDetail){
		YgOrderTrack track=JSON.parseObject(trackOrder, YgOrderTrack.class);
		if(track.getId()!=null){
			track.setUpdateBy(getSession().getTrueName());
			track.setUpdateTime(new Date());
		}else{
			track.setCreateBy(getSession().getTrueName());
			track.setCreateTime(new Date());
		}
		ygOrderTrackService.saveOrUpdate(track);
		return SUCCESS_MESSAGE;		
		
	}
	@RequestMapping("getOrderTrackById")
	@ResponseBody
	public YgOrderTrack getOrderTrackById(Long id){
		
		return ygOrderTrackService.findById(id);
	}

	@RequestMapping("exportOrderTrack")
	public void exportOrderTrack(String q,Date startDate,Date endDate,Boolean isFinished,String srcId,String workType,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		endDate = DateConvertUtils.addDay(endDate, 1);
		if(workType.equals("-1"))workType=null;
		List<Map<String, Object>> list = ygOrderTrackService.searchList(q,startDate,endDate,isFinished,srcId,workType);
		
		byte[] bytes = fileService.downloadFromOss("template/trackorderlist.xlsx"); 
		List<Map<String, Object>> dataMaps = new ArrayList<Map<String, Object>>();
		Map<String, Object> dataMap = new HashMap<String, Object>();

		dataMap.put("resultSet", list);
		dataMaps.add(dataMap);
		DocumentHandler.DownloadExcel(bytes, "订单跟踪.xlsx", dataMap, request, response);
	}

	

}
